package mysprintmvc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ViewResolver {
    private static ViewResolver instance;

    private ViewResolver (){}
    public synchronized static ViewResolver getInstance(){
        if (instance == null){
            instance = new ViewResolver();
        }
        return instance;
    }
    public void resolverView(HttpServletRequest request, HttpServletResponse response,Object viewObj) throws IOException, ServletException {
        if (viewObj !=null){
            String view = (String)viewObj;
            if (view.startsWith("redirect:")){
                String viewValue = view.substring("redirect:".length());
                response.sendRedirect(viewValue);
            }else {
                request.getRequestDispatcher(view).forward(request,response);
            }
        }
    }

}
